<!doctype html>
<html ng-app="app" ng-controller="MainController" ng-click="docClick()">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
  <style>
    body {
      background: black;
      color: lightgreen;
    }
  </style>
</head>
<body>
  <div ng-cloak ng-if="progress()">
    <h1><a href="..">RELEASE COMPLETED</a></h1>
    <audio controls loop>
      <source src="https://s3.amazonaws.com/swarmsim-dev/zadupd02.ogg" type="audio/ogg">
    </audio>
  </div>
  <div ng-cloak ng-if="!progress()">
    <p>No release currently in progress. Refreshing in 60s.</p>
    <p ng-if="versionData">Last release was {{versionData.version}} at {{versionData.updated.format()}}, {{versionData.updated.fromNow()}}.</p>
  </div>
  <div ng-if="!progress">Loading...</div>
</body>
<script>
angular.module('app', []).controller('MainController', function($scope, $location, $timeout, $http) {
  $scope.progress = function() {
    return !!$location.search().progress;
  }
  $scope.nav = function() {
    document.location = '..';
  }
  $scope.docClick = function() {
    if ($scope.progress()) {
      $scope.nav();
    }
  }
  $http.get('../version.json').success(function(data, status, xhr) {
    console.log('versionJson', data);
    $scope.versionData = data;
    data.updated = moment(data.updated);
  });

  if ($scope.progress()) {
    if (!!$location.search().quiet) {
      $scope.nav();
    }
    else {
      $timeout(function(){$('audio')[0].play();}, 0);
    }
    // https://developer.mozilla.org/en-US/docs/Web/API/notification
    function doNotify() {
      var notification = new Notification("Swarmsim release complete", {body:document.location.hostname});
      notification.onclick = function() {
        window.focus();
        $('audio')[0].pause();
        $scope.nav();
      };
    }
    $scope.doNotify = doNotify;
    function checkNotify() {
      // Let's check if the browser supports notifications
      if (!("Notification" in window)) {
        //alert("This browser does not support desktop notification");
      }
    
      // Let's check if the user is okay to get some notification
      else if (Notification.permission === "granted") {
        // If it's okay let's create a notification
        doNotify();
      }
    
      // Otherwise, we need to ask the user for permission
      else if (Notification.permission !== 'denied') {
        Notification.requestPermission(function (permission) {
          // If the user is okay, let's create a notification
          if (permission === "granted") {
            doNotify();
          }
        });
      }
    
      // At last, if the user already denied any notification, and you 
      // want to be respectful there is no need to bother them any more.
    }
    checkNotify();
  }
  $timeout(function(){document.location.reload();}, 60 * 1000);
});
</script>
</html>
